<template>
    <div class="archives card-container">
        <a-tabs type="card" v-model:activeKey="activeKey">
            <a-tab-pane v-for="(item, i) in archivesData" :key="i" :tab="item.title">
                <a-table
                    :columns="columns"
                    :data-source="archivesData[i].currentEsAllInd"
                    :pagination="false"
                    :scroll="scrollComp"
                    bordered
                    :style="{ minHeight: minHeight + 'px' }"
                    size="middle"
                    :loading="tableLoading"
                    :row-class-name="(_record:any, index:number) => (index % 2 === 1 ? 'table-striped' : undefined)"
                />
            </a-tab-pane>
        </a-tabs>
    </div>
</template>

<script setup lang="ts">
    import { getIndicesApi } from "@/api/esArticle";
    import { useScrollxy } from "@/hooks/useEsArticleCommon";
    const { minHeight, scrollComp } = useScrollxy();
    interface ArchivesData {
        title: string;
        currentEsAllInd: Array<{
            "docs.count": string;
            index: string;
        }>;
    }
    const archivesData = ref<Array<ArchivesData>>([]);
    const tableLoading = ref(false);
    const activeKey = ref(0);
    const columns = [
        {
            title: "文章索引",
            key: "index",
            width: 200,
            dataIndex: "index"
        },
        {
            title: "文章数",
            key: "docs.count",
            dataIndex: "docs.count"
        }
    ];

    //获取文章索引列表
    const getESServerAllIndex = async () => {
        tableLoading.value = true;
        getIndicesApi().then((res) => {
            if (res.data.code === 200) {
                const { data } = res.data;
                archivesData.value = Object.keys(data).map((key) => {
                    const currentEsAllInd = Object.keys(data[key]).map((key1) => {
                        return { ...data[key][key1], index: key1 };
                    });
                    return { title: key, currentEsAllInd };
                });
            }
        });
        tableLoading.value = false;
    };
    onMounted(() => {
        //获取es服务全部索引
        getESServerAllIndex();
    });
</script>

<style scoped lang="scss">
    .archives {
        min-width: 1300px;
        margin-top: 20px;
    }
    ::v-deep(.ant-tabs-nav) {
        margin: 0;
    }
</style>
